package com.kcl.dfss;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kcl.dfss.http.ServerConnection;

public class SeclectSeriesActivity extends Activity {

	private LinearLayout select_series_back;
	private ArrayList<HashMap<String, String>> list;
	private String braId,carname;
	private ProgressBar progress_series;
	SeriesAdapter seriesAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_seclect_series);
		
		select_series_back = (LinearLayout) findViewById(R.id.select_series_back);
		select_series_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		progress_series = (ProgressBar) findViewById(R.id.progress_series);
		
		final Handler handler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 1){
					int type[] = new int[list.size()];
					type[0] = 0;
					for(int i=1;i<list.size();i++){
						type[i] = 1;
					}
					ListView list_series = (ListView) findViewById(R.id.list_series);
					seriesAdapter = new SeriesAdapter(SeclectSeriesActivity.this,type);
					list_series.setAdapter(seriesAdapter);
					progress_series.setVisibility(View.GONE);
					list_series.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							Intent intent = new Intent();
							intent.putExtra("serid", list.get(position).get("serId"));
							intent.putExtra("braid", braId);
							intent.setClass(SeclectSeriesActivity.this, SelectModelActivity.class);
							startActivity(intent);
						}
					});
				}
			}
			
		};
		
		Intent intent = getIntent();
		braId = intent.getStringExtra("braId");
		carname = intent.getStringExtra("carname");
		SharedPreferences preferences = getSharedPreferences("shared_login", 0);
		final String userid = preferences.getString("userid", "");
		list = new ArrayList<HashMap<String,String>>();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String getSeries = ServerConnection.GetSeries(userid, braId);
				try {
					JSONObject jsonObject = new JSONObject(getSeries);
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					list = new ArrayList<HashMap<String,String>>();
					HashMap<String, String> map = null;
					map = new HashMap<String, String>();
					map.put("serId", " ");
					map.put("serName", " ");
					map.put("serInitials", " ");
					list.add(map);
					for(int i=0;i<jsonArray.length();i++){
						JSONObject jo = (JSONObject) jsonArray.opt(i);
						map = new HashMap<String, String>();
						map.put("serId", jo.getString("serId"));
						map.put("serName", jo.getString("serName"));
						map.put("serInitials", jo.getString("serInitials"));
						list.add(map);
					}
					Message message = new Message();
					message.what = 1;
					handler.sendMessage(message);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	private class SeriesAdapter extends BaseAdapter{
		
		Context context;
		int type[];
		
		public SeriesAdapter(Context context,int type[]) {
			this.context = context;
			this.type = type;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			if(position == 0){
				return false;
			}
			return super.isEnabled(position);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(type[position] == 0){
				convertView = getLayoutInflater().inflate(R.layout.select_car_title, null);
				TextView text_letter = (TextView) convertView.findViewById(R.id.text_letter);
				text_letter.setText(carname);
			}else{
				convertView = getLayoutInflater().inflate(R.layout.select_car_item, null);
				String car_series = list.get(position).get("serName");
				TextView text_carname = (TextView) convertView.findViewById(R.id.text_carname);
				text_carname.setText(car_series);
			}
			return convertView;
		}
		
	} 

}
